package com.ruoyi.novel.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.novel.domain.NovelDTO;
import com.ruoyi.novel.mapper.AllBookMapper;
import com.ruoyi.novel.mapper.CollectionMapper;
import com.ruoyi.novel.mapper.HomePageMapper;
import com.ruoyi.novel.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private HomePageMapper homePageMapper;

    @Override
    @Transactional
    public AjaxResult addCollection(Long userId, String novelName) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        collectionMapper.addCollection(userId,novelId);
        return AjaxResult.success(homePageMapper.addCollectionNum(novelId));
    }

    @Override
    @Transactional
    public AjaxResult cancelCollection(Long userId, String novelName) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        collectionMapper.cancelCollection(userId,novelId);
        return AjaxResult.success(homePageMapper.reduceCollectionNum(novelId));
    }

    @Override
    public AjaxResult isCollection(Long userId, String novelName) {
        Long novelId = collectionMapper.selectNovelIdByNovelName(novelName);
        return AjaxResult.success(collectionMapper.isCollection(userId,novelId));
    }
}
